package yangtuo.io.service.alpaca.impl;

import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import yangtuo.io.dal.AlpacaTest;
import yangtuo.io.dal.AlpacaTestExample;
import yangtuo.io.dal.mapper.AlpacaTestMapperExt;
import yangtuo.io.service.alpaca.IAlpacaService;

import java.util.List;

/**
 * <pre>
 *   alpaca service impl test
 * </pre>
 * Created by sirius on 2018/9/19.
 *
 * @author sirius
 * @since 1.0
 */
@Service(value = "AlpacaService")
public class AlpacaServiceImpl implements IAlpacaService {

    @Autowired
    private AlpacaTestMapperExt alpacaTestMapperExt;

    @Override
    public List<AlpacaTest> getAllAlpaca(int pageNum, int pageSize) {
        /*
        * 分页组件设置，该分页组件注册到ThreadLocal中，在当前线程中紧跟着的查询sql会被拦截追加分页数据
        */
        PageHelper.startPage(pageNum, pageSize);
        /*
        * 查询操作，不需要关心分页参数,已由 PageHelper 拦截处理
        */
        AlpacaTestExample alpacaTestExample = new AlpacaTestExample();
        List<AlpacaTest> resultList = alpacaTestMapperExt.selectByExample(alpacaTestExample);
        /*
        * 清理当前线程中的分页设置
        */
        //PageHelper.clearPage();
        return resultList;
    }

    /**
     * 事务控制测试
     *
     * @param name
     * @throws Exception
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void saveAlpaca(String name) throws Exception {
        AlpacaTest at = new AlpacaTest();
        at.setName(name);
        at.setId(1002);
        alpacaTestMapperExt.insertSelective(at);
        throw new Exception("测试异常");
    }
}
